package com.managedBean;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

import org.primefaces.model.menu.DefaultMenuColumn;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSeparator;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.Submenu;

import com.utils.JsfUtils;

@SessionScoped
@ManagedBean(name = "menuBean")
public class MenuBean implements Serializable,ActionListener {
/*Cannot remove the same component twice -- issue fixed in 4.0 https://code.google.com/p/primefaces/issues/detail?id=4431*/
	/**
	 * 
	 */
	private static final long serialVersionUID = -2629522293324079682L;
	private org.primefaces.model.menu.DefaultMenuModel model;
	 protected static UIViewRoot uiViewRoot = new UIViewRoot();
	public MenuBean() {
		model = new DefaultMenuModel();

		// First submenu
		DefaultSubMenu submenu = new DefaultSubMenu();
		submenu.setLabel("Fruits & Vegetables");
		submenu.addElement(addMenuItem("Flowers", "#"));
		submenu.addElement(new DefaultSeparator());
		DefaultSubMenu submenu2=new DefaultSubMenu("Fruits");
		submenu2.addElement(addMenuItem("Apples", "#"));
		submenu2.addElement(addMenuItem("Banana", "#"));
		submenu2.addElement(addMenuItem("Exotic Fruits", "#"));
		submenu2.addElement(addMenuItem("Melon", "#"));
		submenu2.addElement(addMenuItem("Oranges & Sweet Lime", "#"));
		submenu2.addElement(addMenuItem("Other Fruits", "#"));
		submenu.addElement(submenu2);
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Vegetables", "#"));
		model.addElement(submenu);

		// Second submenu
		submenu = new DefaultSubMenu();
		submenu.setLabel("Grocery & Staples");
		submenu.addElement(addMenuItem("Dals & Pulses", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Dry Fruits", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Flors & Sooji", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Oils & Ghee", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Organic Staples", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Rice & Rice Products", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Salt & Sugar", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Spices & Masalas", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Whole Grains", "#"));

		model.addElement(submenu);

		// Third submenu
		submenu = new DefaultSubMenu();
		submenu.setLabel("Bread Dairy & Eggs");
		submenu.addElement(addMenuItem("Bread & Bakery", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Dairy Products", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Eggs", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Organic", "#"));
		model.addElement(submenu);

		// Fourth submenu
		submenu = new DefaultSubMenu();
		submenu.setLabel("Beverages");
		submenu.addElement(addMenuItem("Energy & Health Drinks", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Fruit Drinks & Juices", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Packaged Mineral water", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Soft Drinks", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Tea & Coffe", "#"));
		model.addElement(submenu);

		// Fifth submenu
		submenu = new DefaultSubMenu();
		submenu.setLabel("Branded Foods");
		submenu.addElement(addMenuItem("Baby Foods", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Baking Items", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Breakfast Cereals", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Frozen Vegetables", "#"));
		submenu.addElement(new DefaultSeparator());
		submenu.addElement(addMenuItem("Non Veg Canned Food", "#"));
		model.addElement(submenu);
		
        
		DefaultSubMenu voidSubmenu = new DefaultSubMenu("Void Submenu");

		//First submenu
		DefaultSubMenu firstSubmenu = new DefaultSubMenu("Dynamic Submenu");

		DefaultMenuColumn column = new DefaultMenuColumn();

		DefaultMenuItem item = new DefaultMenuItem("External");
		item.setUrl("http://www.primefaces.org");
		item.setIcon("ui-icon-home");
		firstSubmenu.addElement(item);

		column.addElement(firstSubmenu);

		//Second submenu
		DefaultSubMenu secondSubmenu = new DefaultSubMenu("Dynamic Actions");

		item = new DefaultMenuItem("Save");
		item.setIcon("ui-icon-disk");
		item.setCommand("#{mantClienteMB.save}");
		item.setUpdate("messages");
		secondSubmenu.addElement(item);

		item = new DefaultMenuItem("Delete");
		item.setIcon("ui-icon-close");
		item.setCommand("#{menuBean.delete}");
		item.setAjax(false);
		secondSubmenu.addElement(item);

		item = new DefaultMenuItem("Redirect");
		item.setIcon("ui-icon-search");
		item.setCommand("#{menuBean.redirect}");
		secondSubmenu.addElement(item);

		column.addElement(secondSubmenu);
		      
		voidSubmenu.addElement(column);

		model.addElement(voidSubmenu);

	}

	public DefaultMenuModel getModel() {
		return model;
	}

	public void save() {
		addMessage("Data saved");
	}

	public void update() {
		addMessage("Data updated");
	}

	public void delete() {
		addMessage("Data deleted");
	}

	public void addMessage(String summary) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				summary, null);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private Submenu addSubMenu(String itemValue) {
		DefaultSubMenu submenu = new DefaultSubMenu();
		submenu.setLabel(itemValue);
		return submenu;
	}
	private DefaultMenuItem addMenuItem(String itemValue, String url) {
		DefaultMenuItem item = new DefaultMenuItem();
		item.setValue(itemValue);
		item.setUrl("../ui/catalog.jsf?id="+itemValue);
		item.setId("item"+uiViewRoot.createUniqueId());
		item.setAjax(true);
//		item.setActionExpression(JsfUtils.createActionExpression("#{menuBean.navigation}", String.class));
//		item.addActionListener(this);
//		item.setTransient(true);
		return item;
	}

	@Override
	public void processAction(ActionEvent arg0) throws AbortProcessingException {
		String id = (String) FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().get("id");
		System.out.println("Product id="+id);
		ItemsManagedBean bean= (ItemsManagedBean) JsfUtils.getBeanInstanceFromFacesContext("itemsManagedBean");
		bean.setMenuId(id);
		bean.populateItems();
		
	}
}
